<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="3" :xs="20" style="width: 11%;border-right-style: solid;border-width: 1px">
                <div class="head-container">
                    <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick"/>
                </div>
            </el-col>
            <!--数据-->
            <el-col :span="20" :xs="24" style="width: 89%">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/>
                    </el-form-item>
                    <el-form-item label="课程" prop="course">
                        <el-select v-model="queryParams.course" placeholder="请选择课程" clearable>
                            <el-option v-for="dict in dict.type.score_course_list_dict" :key="dict.value" :label="dict.label" :value="dict.value"/>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['score:skill:add']">新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['score:skill:remove']">删除</el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>

                <el-table v-loading="loading" :data="skillList" @selection-change="handleSelectionChange" :height="table_height">
                    <el-table-column type="selection" width="55" align="center"/>
                    <el-table-column label="名称" align="center" prop="name">
                        <template slot-scope="scope">
                            <el-button type="text" @click="toScoreSheet(scope.row)">{{scope.row.name}}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="科室" align="center" prop="deptName"/>
                    <el-table-column label="课程" align="center" prop="course">
                        <template slot-scope="scope">
                            <dict-tag :options="dict.type.score_course_list_dict" :value="scope.row.course"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="学生人数限制" align="center" prop="studentLimit"/>
                    <el-table-column label="考试时长(分)" align="center" :formatter="exDurationFormat" prop="exDuration"/>
                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['score:skill:edit']">修改</el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['score:skill:remove']">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
            </el-col>

        </el-row>


        <!-- 添加或修改技能管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="科室" prop="deptId">
                    <treeselect v-model="form.deptId" :options="deptOptions" :defaultExpandLevel="Infinity" :show-count="true" clearable placeholder="请选择归属科室" style="width: 260px"/>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入名称" style="width: 260px" clearable/>
                </el-form-item>
                <el-form-item label="课程" prop="course">
                    <el-select v-model="form.course" placeholder="请选择课程" style="width: 260px" clearable>
                        <el-option v-for="dict in dict.type.score_course_list_dict" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学生人数" prop="studentLimit">
                    <el-input-number :min="0" v-model="form.studentLimit" placeholder="请输入学生人数限制" style="width: 160px"/>
                </el-form-item>
                <el-form-item label="考试时长(分)" prop="exDuration">
                    <el-input-number :min="0" v-model="form.exDuration" placeholder="请输入考试时长" style="width: 160px"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {listSkill, delSkill, addSkill, updateSkill} from "@/api/score/skill";
import {deptTreeSelect} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {copyObj} from "@/utils/zzDouUtils/objUtil";
import {nowDateStr} from "@/utils/zzDouUtils/dateUtil";


export default {
    name: "Skill",
    dicts: ['score_course_list_dict'],
    components:{Treeselect},
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 技能管理表格数据
            skillList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                deptId: null,
                name: null,
                course: null,
                studentLimit: null,
                exDuration: null,
                reservedOne: null,
                reservedTwo: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                deptId: [
                    {required: true, message: "请选择科室", trigger: "blur"}
                ],
                name: [
                    {required: true, message: "请输入技能名称", trigger: "blur"}
                ],
                course: [
                    {required: true, message: "请选择课程", trigger: "blur"}
                ],
                studentLimit: [
                    {required: true, message: "请输入学生人数限制", trigger: "blur"}
                ],
                exDuration: [
                    {required: true, message: "请输入考试时长", trigger: "blur"}
                ],
            },
            // 科室树选项
            deptOptions: undefined,
            defaultProps: {
                children: "children",
                label: "label"
            },
        };
    },
    created() {
        this.getList();
        this.getDeptTree();
    },
    methods: {
        /** 查询技能管理列表 */
        getList() {
            this.loading = true;
            listSkill(this.queryParams).then(response => {
                this.skillList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 查询科室下拉树结构 */
        getDeptTree() {
            deptTreeSelect().then(response => {
                this.deptOptions = response.data;
            });
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 科室节点单击事件
        handleNodeClick(data) {
            this.queryParams.deptId = data.id;
            this.handleQuery();
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                deptId: null,
                name: null,
                course: null,
                studentLimit: null,
                exDuration: null,
                reservedOne: null,
                reservedTwo: null,
                createTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加技能管理";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.form = copyObj(row);
            this.form.exDuration = row.exDuration / 60;
            this.open = true;
            this.title = "修改技能管理";
            // getSkill(id).then(response => {
            //     this.form = response.data;
            //     this.open = true;
            //     this.title = "修改技能管理";
            // });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.exDuration = this.form.exDuration * 60;
                    if (this.form.id != null) {
                        updateSkill(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSkill(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除技能管理编号为"' + ids + '"的数据项？').then(function () {
                return delSkill(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('score/skill/export', {
                ...this.queryParams
            }, `skill_${nowDateStr()}.xlsx`)
        },
        //----------------------------
        /** 考试时长格式化 */
        exDurationFormat(row, column,cellValue){
            return cellValue / 60;
        },
        /** 跳转下挂评分表页面 */
        toScoreSheet(row){
            this.$tab.openPage(row.name, '/score/sheet/index.vue/'+ row.id, {});
        }
    }
};
</script>
